package com.aca.market.service.impl;

import com.aca.market.entity.Item;
import com.aca.market.entity.Storage;
import com.aca.market.mapper.StorageMapper;
import com.aca.market.service.IStorageService;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

/**
 * <p>
 * 仓储表 服务实现类
 * </p>
 *
 * @author aca
 * @since 2020-11-13
 */
@Transactional
@Service
public class StorageServiceImpl extends ServiceImpl<StorageMapper, Storage> implements IStorageService {

    @Autowired
    StorageMapper storageMapper;

    @Override
    public IPage<Storage> selectPage(Page<Storage> page, Wrapper<Storage> queryWrapper){
        return storageMapper.selectPage(page,queryWrapper);
    }

    @Override
    //itemId:Storage的map
    public Map<Long,Storage> getIdStorageMap(List<Item> items){
        List<Storage> storages = storageMapper.selectList(new QueryWrapper<Storage>()
                .orderByAsc("item_id")
                .in("item_id", items.parallelStream().mapToLong(Item::getId)
                        .boxed().collect(Collectors.toList())));
        Map<Long,Storage> map = new ConcurrentHashMap<>();
        storages.parallelStream().forEach(s-> map.put(s.getItemId(),s));
        return map;
    }

    @Override
    //检查有无低于库存警戒线的商品
    public List<Storage> checkStorage(){
        return storageMapper.checkStorage();
    }
}
